home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 028a / dezip20.zip / MEMALLOC.PAS < prev    next >
Pascal/Delphi Source File  |  1989-07-05  |  4KB  |  89 lines

  1. Unit MemAlloc;
  2.  
  3. { Purpose is to provide the ability to create (destroy) dynamic variables  }
  4. { without needing to reserve heap space at compile time.                   }
  5.  
  6. Interface
  7.  
  8. Function Malloc(Var Ptr; Size : Word) : Word;
  9. { Allocate free memory and return a pointer to it.  The amount of memory      }
  10. { requested from DOS is calculated as (Size/4)+1 paragraphs.  If the          }
  11. { allocation is successful, the untyped VAR parameter Ptr will be populated   }
  12. { with the address of the allocated memory block, and the function will return}
  13. { a zero result.  Should the request to DOS fail, Ptr will be populated with  }
  14. { the value NIL, and the function will return the appropriate DOS error code. }
  15.  
  16. Function Dalloc(Var Ptr) : Word;
  17. { Deallocate the memory pointed to by the untyped VAR parameter Ptr           }
  18.  
  19. Function DosMemAvail : LongInt;
  20. { Return the size of the largest contiguous chuck of memory available for use }
  21.  
  22. { --------------------------------------------------------------------------- }
  23.  
  24. Implementation
  25.  
  26. { --------------------------------------------------------------------------- }
  27.  
  28. Function Malloc(Var Ptr; Size : Word) : Word;
  29. Begin
  30.    Inline(
  31.      $8B/$46/<SIZE/         {            mov         ax,[bp+<Size]}
  32.      $B9/$04/$00/           {            mov         cx,4}
  33.      $D3/$E8/               {            shr         ax,cl}
  34.      $40/                   {            inc         ax}
  35.      $89/$C3/               {            mov         bx,ax}
  36.      $B4/$48/               {            mov         ah,$48}
  37.      $CD/$21/               {            int         $21             ;Allocate memory}
  38.      $72/$07/               {            jc          AllocErr        ;If any errors ....}
  39.      $C7/$46/$FE/$00/$00/   {NoErrors:   mov word    [bp-2],0        ;Return 0 for successful allocation}
  40.      $EB/$05/               {            jmp short   Exit}
  41.      $89/$46/$FE/           {AllocErr:   mov         [bp-2],ax       ;Return error code}
  42.      $31/$C0/               {            xor         ax,ax           ;Store a NIL value into the ptr}
  43.      $C4/$7E/<PTR/          {Exit:       les         di,[bp+<Ptr]    ;Address of pointer into es:di}
  44.      $50/                   {            push        ax              ;Save the Segment part}
  45.      $31/$C0/               {            xor         ax,ax           ;Offset is always 0}
  46.      $FC/                   {            cld                         ;Make sure direction is upward}
  47.      $AB/                   {            stosw                       ;Store offset of memory block}
  48.      $58/                   {            pop         ax              ;Get back segment part}
  49.      $AB);                  {            stosw                       ;Store segment of memory block}
  50.    
  51. End {Malloc};
  52.  
  53. { --------------------------------------------------------------------------- }
  54.  
  55. Function Dalloc(Var Ptr) : Word;
  56. Begin
  57.    If Pointer(Ptr) <> NIL then begin
  58.       Inline(
  59.         $B4/$49/               {            mov         ah,$49}
  60.         $C4/$7E/<PTR/          {            les         di,[bp+<Ptr]}
  61.         $26/$C4/$3D/           {        es: les         di,[di]}
  62.         $CD/$21/               {            int         $21}
  63.         $72/$02/               {            jc          Exit}
  64.         $31/$C0/               {NoError:    xor         ax,ax}
  65.         $89/$46/$FE);          {Exit:       mov         [bp-2],ax}
  66.       Pointer(Ptr) := NIL;
  67.    end {if}
  68.    else
  69.       Dalloc := 0;
  70. End {Dealloc};
  71.  
  72. { --------------------------------------------------------------------------- }
  73.  
  74. Function DosMemAvail : LongInt;
  75. Begin
  76.    Inline(
  77.      $BB/$FF/$FF/           {         mov         bx,$FFFF}
  78.      $B4/$48/               {         mov         ah,$48}
  79.      $CD/$21/               {         int         $21}
  80.      $89/$D8/               {         mov         ax,bx}
  81.      $B9/$10/$00/           {         mov         cx,16}
  82.      $F7/$E1/               {         mul         cx}
  83.      $89/$46/$FC/           {         mov         [bp-4],ax}
  84.      $89/$56/$FE);          {         mov         [bp-2],dx}
  85. end {DosMemAvail};
  86.  
  87. End {Unit MemAlloc}.
  88.  
  89.